<script lang="ts" setup>
import {totaldata} from "../../api/order";
import * as echarts from 'echarts'
import {onBeforeUnmount, ref} from "vue";

let charts: any = ref('')
let emain: any = ref(null)

async function getData() {
  let res = await totaldata()
  let {xData, amountData, orderData} = res.data
  drawPie(xData, amountData, orderData)
}

function drawPie(xData: any, amountData: any, orderData: any) {
  charts.value = echarts.init(emain.value)
  charts.value.setOption({
    backgroundColor: '#FFF',
    title: {
      text: '数据统计'
    },
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      // data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    toolbox: {
      feature: {
        saveAsImage: {}
      }
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: xData
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '金额数据',
        type: 'line',
        data: amountData
      },
      {
        name: '订单数据',
        type: 'line',
        data: orderData
      },
    ]
  })
  window.addEventListener('reset', charts.resize)
}

getData()
onBeforeUnmount(() => {
  window.removeEventListener('reset', charts.resize)
})
</script>
<template>
  <div id="emain" ref="emain" style="width: 100%;height: 400px"></div>
</template>
<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  list-style: none;
}
</style>